package com.ifast.common.utils;

import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author :luzq
 * @date:2019/7/11 0011 上午 9:40
 * @copyright: @2018
 *
 * 使用枚举做单利模式，线程安全的,推荐这种方式
 */
public class ExePoolUtil2 {


    public static Executor getIns(){
        return Singleton2.INS.getIns();
    }

    private enum Singleton2{
        INS;

        private Executor executor;

        //构造函数，JVm保证这个方法被调用一次绝对的
        Singleton2(){
            int cpusl = Runtime.getRuntime().availableProcessors();
            executor =  new ThreadPoolExecutor(cpusl, cpusl * 2,
                    0L, TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue<Runnable>(50),
                    new TaskMyRejected());
        }

        public Executor getIns(){
            return executor;
        }
    }

}
